package com.jeecms.wicool.dao.product.impl;


import com.google.common.primitives.Ints;
import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.product.ProductDao;
import com.jeecms.wicool.entity.product.Product;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductDaoImpl extends HibernateBaseDao<Product, Integer>
        implements ProductDao {

    @Override
    protected Class<Product> getEntityClass() {
        // TODO Auto-generated method stub
        return Product.class;
    }

    @Override
    public Pagination getPage(Integer productCateId, String productName,
                              int pageNo, int pageSize, CmsUser user) {
        Finder f = Finder.create("select bean from Product bean where 1=1 ");

        if (null != productCateId) {
            f.append(" and bean.productCate.id = :productCateId");
            f.setParam("productCateId", productCateId);
        }
        if (!StringUtils.isBlank(productName)) {
            f.append(" and bean.productName like :productName");
            f.setParam("productName", "%" + productName + "%");
        }

        if (user != null && user.getShopId() != null) {
            f.append(" and bean.shop.id = :shopId");
            f.setParam("shopId", user.getShopId());
        }
        f.append(" order by bean.orderNum desc");
        return find(f, pageNo, pageSize);
    }

    public Pagination getPageForVip(Integer productCateId, String productName,
            int pageNo, int pageSize, CmsUser user){
    	Finder f = Finder.create("select bean from Product bean where bean.productCate.id=4 ");

        if (null != productCateId) {
            f.append(" and bean.productCate.id = :productCateId");
            f.setParam("productCateId", productCateId);
        }
        if (!StringUtils.isBlank(productName)) {
            f.append(" and bean.productName like :productName");
            f.setParam("productName", "%" + productName + "%");
        }

        if (user != null && user.getShopId() != null) {
            f.append(" and bean.shop.id = :shopId");
            f.setParam("shopId", user.getShopId());
        }
        f.append(" order by bean.orderNum desc");
        return find(f, pageNo, pageSize);
    }
    @Override
    public Product findById(Integer id) {
        Product entity = get(id);
        return entity;
    }

    @Override
    public Product save(Product bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public Product deleteById(Integer id) {
        Product entity = super.get(id);
        if (entity != null) {
            getSession().delete(entity);
        }
        return entity;
    }

    @Override
    public Integer getMaxItemNo() {
        Query query = getSession().createQuery("select max(p.itemNo) from Product p");
        return null == query.uniqueResult() ? 0 : Ints.tryParse(query.uniqueResult().toString());
    }

    @Override
    public List<Product> getAll(Integer productCateId) {
        Finder f = Finder.create("select bean from Product bean where 1=1 and bean.productCate.id = :productCateId").setParam("productCateId", productCateId);
        return find(f);
    }
}